﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// qiugou_detail 的摘要说明
    /// </summary>
    public class xinxianshi_detail : IHttpHandler, IRequiresSessionState
    {
        protected int id = 0;
        public void ProcessRequest(HttpContext context)
        {
            //检查用户是否登录
            int _user_id = DTRequest.GetFormInt("txtUserID", 0);

            id = DTRequest.GetQueryInt("id", 0);
            if (id == 0)
            {
                return;
            }
            BLL.xinxianshi bll = new BLL.xinxianshi();
            Model.xinxianshi model = bll.GetModel(id);
            //更新阅读数
            bll.UpdateField(id, "hits=hits+1");
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            strTxt.Append("\"remark\":\"" + Microsoft.JScript.GlobalObject.escape(model.remark.ToString()) + "\"");
            strTxt.Append(",\"user_id\":" + model.user_id);
            if (model.user_id > 0)
            {
                string u_name = new BLL.users().GetUserName(model.user_id);
                string user_avatar = new BLL.users().GetAvatar(model.user_id);
                strTxt.Append(",\"user_name\":\"" + u_name + "\"");
                if (string.IsNullOrEmpty(user_avatar))
                {
                    strTxt.Append(",\"user_avatar\":\"../image/default_avatar.png\"");
                }
                else
                {
                    if (user_avatar.Contains("shicaiquanzi"))
                    {
                        strTxt.Append(",\"user_avatar\":\"http:\\/\\/" + user_avatar + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"user_avatar\":\"http:\\/\\/www.shicaiquanzi.com" + user_avatar + "\"");
                    }
                    
                }
            }
            strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(model.add_time)) + "\"");
            strTxt.Append(",\"hits\":" + model.hits);
            strTxt.Append(",\"zan\":" + model.zan);
            strTxt.Append(",\"comment\":" + model.comment);

            string imglist = string.Empty;
            if (model.albums != null)
            {
                foreach (Model.xxs_albums modelt in model.albums)
                {
                    if (modelt.original_path.Contains("shicaiquanzi"))
                    {
                        imglist += "<img src=\"http://" + modelt.original_path + "\"/>";
                    }
                    else
                    {
                        imglist += "<img src=\"http://www.shicaiquanzi.com" + modelt.original_path + "\"/>";
                    }
                }
            }
            if (!string.IsNullOrEmpty(imglist))
            {
                strTxt.Append(",\"imglist\":\"" + Microsoft.JScript.GlobalObject.escape(imglist) + "\"");
            }
            //判断是否有传过来用户名、密码
            if (new BLL.zan().Exists(id, _user_id))
            {
                strTxt.Append(",\"is_zan\":1");
            }
            else
            {
                strTxt.Append(",\"is_zan\":0");
            }

            //加载评论列表
            strTxt.Append(",\"comment_list\":[");
            BLL.comment bll2 = new BLL.comment();
            DataSet ds = bll2.GetList(0, "status=0 and article_id=" + id, "id desc");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"user_id\":" + dr["user_id"]);
                    if (Convert.ToInt32(dr["user_id"]) > 0)
                    {
                        string uname = new BLL.users().GetUserName(Convert.ToInt32(dr["user_id"]));
                        string user_avatar = new BLL.users().GetAvatar(Convert.ToInt32(dr["user_id"]));
                        strTxt.Append(",\"user_name\":\"" + uname + "\"");
                        if (string.IsNullOrEmpty(user_avatar))
                        {
                            strTxt.Append(",\"user_avatar\":\"../image/default_avatar.png\"");
                        }
                        else
                        {
                            if (user_avatar.Contains("shicaiquanzi"))
                            {
                                strTxt.Append(",\"user_avatar\":\"http:\\/\\/" + user_avatar + "\"");
                            }
                            else
                            {
                                strTxt.Append(",\"user_avatar\":\"http:\\/\\/www.shicaiquanzi.com" + user_avatar + "\"");
                            }
                        }
                    }
                    strTxt.Append(",\"remark\":\"" + Microsoft.JScript.GlobalObject.escape(dr["remark"]) + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
            }
            strTxt.Append("]");
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}